//
//  VersionUpdatePopView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/5/17.
//

import Foundation
import UIKit
import JFPopup
import SafariServices

class VersionUpdateManager: NSObject {
    static let shared = VersionUpdateManager()

    var isShow = false
    
    func setupVersion() {
        let build = Bundle.main.infoDictionary?["CFBundleVersion"] as! String
        
        API.getAppVersion { responseModel in
            if let model = responseModel.model, let versionCode = model.versionCode {
                if versionCode > Int64(build)! {
                    if self.isShow == true {
                        return
                    }
                    self.updateUI(withModel: model)
                }
            }
        }
    }
    
    func updateUI(withModel model: AppVersionModal) {
        Utils.app.window?.popup.dialog(with: false, bgColor: .black.withAlphaComponent(0.65)) { mainContainer in
            let v = VersionUpdatePopView(frame: CGRect(x: 0, y: 0, width: 300, height: 362), model: model)
            v.didDismissWith = { () -> () in
                self.isShow = false
                mainContainer?.dismissPopupView(completion: { isBlock in })
            }
            return v
        }
    }
}

class VersionUpdatePopView: UIView {
    open var didDismissWith:(() -> ())?

    var model: AppVersionModal?
    
    init(frame: CGRect, model: AppVersionModal?) {
        super.init(frame: frame)
        setupUI()
        
        self.model = model
        
        if model?.sfqzgx == 0 {
            forceUpdated()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 点击确认按钮
    @objc private func confirmToggle() {
        if let downURL = model?.url, let url = URL(string: downURL) {
            UIApplication.shared.open(url)
        }
        
        didDismissWith?()
    }
    
    @objc func closeToggle() {
        didDismissWith?()
    }
    
    func forceUpdated() {
        closeBtn.isHidden = true
        
        confirmBtn.snp.remakeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-22)
            make.centerX.equalToSuperview()
            make.width.equalTo(113)
            make.height.equalTo(40)
        }
    }

    func setupUI() {
        addSubview(baseIV)
        baseIV.snp.makeConstraints { (make) -> Void in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(362)
        }
        
        addSubview(closeBtn)
        closeBtn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-22)
            make.left.equalToSuperview().offset(30)
            make.width.equalTo(113)
            make.height.equalTo(40)
        }
        
        addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-22)
            make.right.equalToSuperview().offset(-30)
            make.width.equalTo(113)
            make.height.equalTo(40)
        }
    }
    
    lazy var baseIV: UIImageView = {
        let v = UIImageView(image: UIImage(named: "ic_VersionUpdate"))
        return v
    }()
    
    lazy var confirmBtn: UIButton = {
        let btn = UIButton.createPrimaryLarge("立即更新")
        btn.addTarget(self, action: #selector(confirmToggle), for: .touchUpInside)
        return btn
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton.createDefaultLarge("稍后更新")
        btn.addTarget(self, action: #selector(closeToggle), for: .touchUpInside)
        return btn
    }()
}
